package com.oschina.wed.service.impl;

import com.oschina.wed.common.enums.VoucherFlowDirectionEnum;
import com.oschina.wed.common.utils.CommonUtils;
import com.oschina.wed.dal.entity.AccountVoucherFlow;
import com.oschina.wed.dal.entity.AccountVoucherSumTmp;
import com.oschina.wed.service.AccountVoucherService;
import com.taobao.pamirs.schedule.IScheduleTaskDealMulti;
import com.taobao.pamirs.schedule.TaskItemDefine;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Hashtable;
import java.util.List;

@Component("accountVoucherService")
public class TBScheduleExcuterServiceImpl implements IScheduleTaskDealMulti<AccountVoucherFlow> {

    @Resource
    private AccountVoucherService accountVoucherService;

    @Override
    public boolean execute(AccountVoucherFlow[] accountVoucherFlows, String s) throws Exception {

        Hashtable<Long, AccountVoucherSumTmp> sum = new Hashtable<>();

        String voucherNos = "";

        for (AccountVoucherFlow item : accountVoucherFlows) {
            if (sum.containsKey(item.getAccountNo())) {
                switch (VoucherFlowDirectionEnum.getByDirectionCode(item.getAccountDirection())) {
                    case IN:
                        sum.get(item.getAccountNo()).addAmount(item.getAmount());
                        break;
                    case OUT:
                        sum.get(item.getAccountNo()).subAmount(item.getAmount());
                        break;
                }
            } else {
                AccountVoucherSumTmp tmp = new AccountVoucherSumTmp();
                tmp.setAccountNo(item.getAccountNo());
                tmp.setAccountingDate(new SimpleDateFormat("yyyyMMdd").format(item.getPayDate()));
                switch (VoucherFlowDirectionEnum.getByDirectionCode(item.getAccountDirection())) {
                    case IN:
                        tmp.setIncomeAmount(item.getAmount());
                        break;
                    case OUT:
                        tmp.setExpendAmount(item.getAmount());
                        break;
                }
                sum.put(item.getAccountNo(), tmp);
            }

            voucherNos = voucherNos.concat(item.getVoucherNo().toString()).concat(",");
        }

        for (AccountVoucherSumTmp accountVoucherSumTmp : sum.values()) {
            Hashtable<String, Object> sumVoucher = CommonUtils.convert2HashTable(accountVoucherSumTmp);

            if (accountVoucherService.updateAccountVoucherSum(sumVoucher) <= 0) {
                accountVoucherService.insertAccountVoucherSum(sumVoucher);
            }
        }

        accountVoucherService.update2Staticed(voucherNos.substring(0, voucherNos.length() - 1));
        return true;
    }

    @Override
    public List<AccountVoucherFlow> selectTasks(String taskParameters, String owenrSign, int taskNum, List<TaskItemDefine> list, int pageSize) throws Exception {
        System.out.println("分片：" + list.get(0) + "  计算启动....");

        Hashtable<String, Object> query = new Hashtable<>();
        query.put("dataIndex", taskNum);
        query.put("targetIndex", list.get(0).getTaskItemId());
        query.put("skipNum", 0);
        query.put("pageSize", pageSize);
        return accountVoucherService.selectForStatics(query);
    }

    @Override
    public Comparator<AccountVoucherFlow> getComparator() {
        //通常不实现
        return null;
    }
}
